package com.zn.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import sun.misc.BASE64Decoder;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.util.*;
import java.util.logging.Logger;

public class AESEncrypt {



    /**
     * h5 AES加密为base 64 code
     * @param content 待加密的内容
     * @param encryptKey 加密密钥
     * @return 加密后的base 64 code
     * @throws Exception
     */
    public static String aesEncrypt(String content, String encryptKey) throws Exception {
        return base64Encode(aesEncryptToBytes(content, encryptKey));
    }

    /**
     * h5 base 64 encode
     * @param bytes 待编码的byte[]
     * @return 编码后的base 64 code
     */
    public static String base64Encode(byte[] bytes){
        return Base64.encodeBase64String(bytes);
    }

    /**
     * h5 AES加密
     * @param content 待加密的内容
     * @param encryptKey 加密密钥
     * @return 加密后的byte[]
     * @throws Exception
     */
    public static byte[] aesEncryptToBytes(String content, String encryptKey) throws Exception {
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        kgen.init(128);
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(encryptKey.getBytes(), "AES"));
        return cipher.doFinal(content.getBytes("utf-8"));
    }

    /**
     * h5 将base 64 code AES解密
     * @param encryptStr 待解密的base 64 code
     * @param decryptKey 解密密钥
     * @return 解密后的string
     * @throws Exception
     */
    public static String aesDecrypt(String encryptStr, String decryptKey) throws Exception {
        return StringUtils.isEmpty(encryptStr) ? null : aesDecryptByBytes(base64Decode(encryptStr), decryptKey);
    }

    /**
     * h5 AES解密
     * @param encryptBytes 待解密的byte[]
     * @param decryptKey 解密密钥
     * @return 解密后的String
     * @throws Exception
     */
    public static String aesDecryptByBytes(byte[] encryptBytes, String decryptKey) throws Exception {
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        kgen.init(128);
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(decryptKey.getBytes(), "AES"));
        byte[] decryptBytes = cipher.doFinal(encryptBytes);
        return new String(decryptBytes);
    }

    /**
     * h5 base 64 decode
     * @param base64Code 待解码的base 64 code
     * @return 解码后的byte[]
     * @throws Exception
     */
    public static byte[] base64Decode(String base64Code) throws Exception{
        return StringUtils.isEmpty(base64Code) ? null : new BASE64Decoder().decodeBuffer(base64Code);
    }

    /**
     * 测试
     */
//    public static void main(String[] args) {
//        try {
//            String key="ideal@(zhenniub)";
//            System.out.println("请输入密文:");
//            Scanner scanner = new Scanner(System.in);
//            String entypt = scanner.next();
//            String decryptStr=aesDecrypt(entypt,key);
//            System.err.println(decryptStr);
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//
//    }
    public static void main(String[] args) throws Exception {
       // String content = "{\"loginId\":\"18155301635\",\"sessionId\":\"87b18c1602e049499fddeeee58a27230\",\"flowType\":\"\",\"skip\":\"0\",\"pageSize\":\"10\"}";
//        String content="{\n" +
//                "        \"customerTradeAccount\":\"a10001040\",\n" +
//                "        \"customerTradePassword\":\"123456\"\n" +
//                "}";
        String content="15026552403";
        String key="ideal@(zhenniub)";
        String enctyptStr=aesEncrypt(content, key);
        System.err.println(enctyptStr);
        String entypt="yjqF+cjRI7QqvpYYt2CuOl8YDmw5Vjeo3tREyeJAI81FIFdVUg3H2Ex2neTeJ1NnkyLuPtmLK4g7jf5F/u6f622UHrs+qySIFW2wGjWz1wjxgeWYmelY4l7oubIoGj1uBG0q7Vro1fMFlqg03M+uhA==";
//        String entypt="BfZZzRrXcF2Fthyz8qLuBtRj2FK2dsv4sL/NwpLtwb8XTqpn0ksJGQ1yVidCRL/1i/D5bhELkFl1sdmggtGUus30KWV8AUzY2py5ZaP0Ou8=";
        String decryptStr=aesDecrypt(entypt,key);
        System.err.println(decryptStr);
        System.err.println("DateUtils.parseToDateTimeStr(new Date())"+DateUtils.parseToDateTimeStr(new Date()));
        System.err.println("DateUtils.getTradeTime()"+DateUtils.getTradeTime());

//        JSONObject jsonObject = JSONObject.parseObject(decryptStr);
//        Map<String, Object> valueMap = new HashMap<String, Object>();
//        valueMap.putAll(jsonObject);
//        Set<String> setKey=valueMap.keySet();
//        for(String str:setKey){
//            System.err.println("key:"+str+",value:"+valueMap.get(str));
//        }
    }

}
